package com.sf.trtms.trade.personal.utils;

import java.util.Calendar;
import java.util.Date;

import com.sf.shiva.trtms.ground.core.utils.DateUtils;

public class MonthWeekUtils {
	
	private MonthWeekUtils() {
		
	}
	
	/**
	 * 获取date所在月的第一天和最后一天
	 * @param date
	 * @return 例如：[2019-05-01,2019-05-31]
	 */
	public static String[] getFirstLastDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);      
        Date firstDay = c.getTime();
        c.roll(Calendar.DAY_OF_MONTH, -1);
        Date lastDay = c.getTime();		
		
        return new String[] {
			DateUtils.format(firstDay),
			DateUtils.format(lastDay)	
		};
	}
	
	/**
	 * 当前月的第一天和最后一天
	 * @return 例如：[2019-05-01,2019-05-31]
	 */
	public static String[] getFirstLastDateOfCurMonth() {
		return getFirstLastDateOfMonth(new Date());
	}
	
	/**
	 * 下个月的第一天和最后一天
	 * @return 例如：[2019-06-01,2019-06-30]
	 */
	public static String [] getFirstLastDateOfNextMonth() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, 1);//下个月
		
		return getFirstLastDateOfMonth(c.getTime());
	}
	
	/**
	 * 返回当前周的[第一天（周一）,最后一天（周日）]
	 * @return 例如：[2019-05-12,2019-05-18]
	 */
	public static String[] getFirstLastDateOfCurWeek() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date firstDay = c.getTime();
        c.add(Calendar.DAY_OF_MONTH, 6);//加6天就是周日
        Date lastDay = c.getTime();

		return new String[] {
			DateUtils.format(firstDay),
			DateUtils.format(lastDay)	
		};
	}

}
